Page({
  data: {
    jingtou: 'front',
    photograph: true,      // 拍照
    showFacialDottedLine:true,   //面部虚线
    remake:false,    //重拍
    analysis: false,      //分析

    url_temp: 'null',     //照片
    quality: 'normal',
    isBtnsShow: true,
    sbFail: false,       //识别失败的弹窗
    
  }, 


  //拍照
  takePhoto(){
    //console.log(1)
    //my.showToast({content:"拍照！！！"});
    getApp().dataMd(16,`拍照按钮的点击`);

    this.camera_take.takePhoto({
        quality:this.data.quality,
        success: (res)=>{

          //my.alert({ content: "照片!"+JSON.stringify(res.tempImagePath)});
          
          this.setData({
            url_temp: res.tempImagePath,
            photograph: false,
            showFacialDottedLine:false,
            remake: true
          })

          //my.alert({ content: "拍照成功!"+JSON.stringify(res)});
           
        },
        fail: (err) => {
          my.alert({
            content: 'takePhoto error' + JSON.stringify(err) 
          });
        }
      })
  },

  //重新拍照
  takeAnotherPhoto(){
    getApp().dataMd(17,`重新拍照按钮的点击`);
    this.setData({
      url_temp: 'null',
      photograph: true,
      remake: false,
      analysis:false,
      showFacialDottedLine: true,
      sbFail: false
    })
  },

  //确定照片
  determinePhotos(){
    getApp().dataMd(18,`确定照片按钮的点击`);

    // let eyeTypeArr=["桃花眼","瑞凤眼","睡凤眼","柳叶眼","杏眼","狐狸眼","铜铃眼","龙眼","丹凤眼","小鹿眼"];
    // let i = eyeTypeArr[Math.floor((Math.random()*eyeTypeArr.length))];

    this.setData({
      photograph:false,
      remake:false,
      analysis:true
    });

      my.showLoading();

      my.compressImage({
        apFilePaths:[this.data.url_temp],
        compressLevel:4,
        success:(res)=>{

          //my.alert({content:`压缩===>${JSON.stringify(res)}`});
          
          let imgs = res.apFilePaths[0];

          getApp().cloud.file.uploadFile({  
            filePath: imgs,  
            fileType: 'image', 
          }).then(res => {

            this.setData({
              eyeTestImgUrl:res.url
            })

            //my.alert({content:`图片链接 ${JSON.stringify(this.data.eyeTestImgUrl)}`})

            //my.hideLoading();
            
            getApp().cloud.application.httpRequest({
              'path' : `/api/c/ar/eye?imgUrl=${this.data.eyeTestImgUrl}`, 
              'method':'get',
              'headers':{ "Content-Type":"application/json" },
              'params':{},
              'body':{},
              'exts':{}  }).then(res => {
              
              //my.alert({content:`cccc-====眼型结果${JSON.stringify(res)}`});

              if(res.data.result == ''){

                my.hideLoading();
                
                //识别失败
                this.setData({
                  sbFail: true
                })

              }else{

                //my.alert({content:`眼型结果${JSON.stringify(res.data.result[0])}`});

                let eyeTestResult = res.data.result[0]
                
                // 保存结果
                getApp().cloud.function.invoke('client',{ eyesType: res.data.result[0].eye_type},'editAReyes').then(res => {
                  console.log('眼型修改',res);  
                  //my.showToast({content:"获取用户信息"});
                  if(res.code == 200){
                    //测眼型参与人数埋点
                      getApp().dataMd(11,"测眼型参与人数");

                      getApp().eyeTestResult = eyeTestResult;

                      for (var i = 0;i < res.data.length;i++) {
                        var goods = res.data[i]
                        if(goods.image == null || goods.image == '' || goods.link == null || goods.link == ''){
                          res.data.splice(i,1);
                          i--;
                        }
                      }

                      getApp().goodsArrs = res.data

                      // my.alert({
                      //   content:`眼型修改:${JSON.stringify(res)}`
                      // });

                      my.hideLoading();

                      // return;

                      setTimeout(()=>{
                        my.redirectTo({url:'/pages/resultsPage/resultsPage'})
                      },500)

                      
                      // getApp().cloud.function.invoke('client',{},'getAReyesList').then(res => {
                      //   if(res.code == 200){
                      //     my.alert({
                      //       content:`眼型相关商品推荐：${JSON.stringify(res)}`
                      //     });


                      //     my.hideLoading();

                      //     return;

                      //     setTimeout(()=>{
                      //       my.redirectTo({url:'/pages/resultsPage/resultsPage'})
                      //     },500)


                      //   }else{
                      //     my.alert({
                      //       content:`网络异常:${JSON.stringify(res)}`
                      //     })
                      //   }
                      // })
                     
  
                  }else{
                    my.showToast({content:'网络异常'})
                  }

                })   

              }
            })
        
          })
        }
      })

      return;

  },


  contactService(){
    getApp().dataMd(19,`联系客服按钮的点击`);
    //联系客服
    my.tb.openMessage({
      sellerNick:"wosado旗舰店",
      success: (res) => {
        console.log(res);
      },
      fail: (res) => {
      console.log(res);
      },
    })
  },

  // onReady(){
  //   my.watchShake({
  //     success: function() {
  //         console.log('动起来了')
  //       my.alert({ title:'动起来了 o.o'});
  //     }
  //   });
  // },


  onLoad() {
    this.camera_take = my.createCameraContext('camera_take');
  },

});
